package com.archgeek.bigdata.presto;


import com.facebook.presto.client.ClientSession;
import com.facebook.presto.client.StatementClient;
import com.facebook.presto.client.StatementClientFactory;
import io.airlift.units.Duration;
import okhttp3.OkHttpClient;

import java.net.URI;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author pizhihui
 * @date 2022-03-02
 */
public class PrestoClientTest {

    public static void main(String[] args) {


        String connectionUrl = "http://192.168.31.103:9666";
        String catalog = "hive";
        String schema = "default";

        ClientSession clientSession = new ClientSession(
                URI.create(connectionUrl),
                "biadmin",
                "",
                Optional.of("test"),
                Collections.emptySet(),
               "",
                catalog,
                schema,
                ZoneId.systemDefault().getId(),
                Locale.getDefault(),
                Collections.emptyMap(),
                Collections.emptyMap(),
                Collections.emptyMap(),
                Collections.emptyMap(),
                Collections.emptyMap(),
                null,
                new Duration(2.0, TimeUnit.MINUTES),
                true,
                Collections.emptyMap(),
                Collections.emptyMap()
        );

        OkHttpClient okHttpClient = new OkHttpClient().newBuilder().build();

        String sql = "show tables";
        String sql2 = "select * from t_access_text";

        StatementClient client = StatementClientFactory
                .newStatementClient(okHttpClient, clientSession, sql2);

//        for (Column column : client.currentStatusInfo().getColumns()) {
//            System.out.println(column.getName());
//        }

        while (client.isRunning()) {
            Iterable<List<Object>> data = client.currentData().getData();
            if (null != data) {
                for (List<Object> datum : data) {
                    System.out.println("有数据了...... " + datum);
                }
            }
            client.advance();
        }

    }

}
